<?php
/**
 * @author Mazvv
 * @package Demand
 */
class Demands extends CRM_Controller {
	public function __construct(){
		parent::__construct();
		$this->a_identifier = 'demands';
		$this->lang->load('clients');
		$this->lang->load('demands');
		$this->load->model('demands_model');
		$this->load->model('currencies_model');
		$this->load->model('advertisessources_model');
		$this->load->model('touroperators_model');
		$this->SetNewToolSize('700x500');
		$this->SetEditToolSize('700x500');
		$this->SetDetailsToolSize('700x750');
		$this->SetFindToolSize('700x750');
		
		
		// Header
		$this->a_validation_rules['date_doc'] = "required";
		$this->a_validation_rules['_demandsstatuses_rid'] = "required";
		$this->a_validation_rules['_currencies_rid'] = "required";  
		$this->a_validation_rules['_zk_rid'] = "required"; 
		$this->a_validation_rules['_clients_rid'] = "required"; 
		$this->a_validation_rules['summ'] = "required"; 
		$this->a_validation_rules['summ_text'] = "required"; 
		$this->a_validation_rules['summ_pay'] = "required";
		// Tours
		$this->a_validation_rules['_touroperators_rid'] = "required";         
		$this->a_validation_rules['_food_rid'] = "required";       
		$this->a_validation_rules['_rooms_rid'] = "required";
		$this->a_validation_rules['room_cat'] = "required";
		$this->a_validation_rules['_countries_rid'] = "required";
		$this->a_validation_rules['_curourts_rid'] = "required";
		$this->a_validation_rules['_advertisessources_rid'] = "required";
		$this->a_validation_rules['_hotelscats_rid'] = "required";
		$this->a_validation_rules['city_name'] = "required";
		$this->a_validation_rules['hotel_name'] = "required";
		$this->a_validation_rules['date_from'] = "required";
		$this->a_validation_rules['date_to'] = "required"; 
		$this->a_validation_rules['transfer'] = "required";
		$this->a_validation_rules['excursions'] = "required";  		
		// Rows
		$this->a_validation_rules['_clients_rid'] = "required";
		 
		// Header
		$this->a_validation_fields['date_doc'] = $this->lang->line('DATE_DOC');
		$this->a_validation_fields['_demandsstatuses_rid'] = $this->lang->line('STATUS');
		$this->a_validation_fields['_currencies_rid'] = $this->lang->line('CURRENCY');  
		$this->a_validation_fields['agreement'] = $this->lang->line('AGREEMENT'); 
		$this->a_validation_fields['summ'] = $this->lang->line('SUMM'); 
		$this->a_validation_fields['summ_text'] = $this->lang->line('SUMM_TEXT'); 
		$this->a_validation_fields['summ_pay'] = $this->lang->line('SUMM_PAY');
		$this->a_validation_fields['summ_tour'] = $this->lang->line('OPERATOR_SUMM');
		$this->a_validation_fields['order_summ'] = $this->lang->line('ORDERSUMM');
		$this->a_validation_fields['curr_cource'] = $this->lang->line('CURR_COURCE');
		$this->a_validation_fields['discount_per'] = $this->lang->line('DISCOUNT_PER');
		$this->a_validation_fields['discount_fix'] = $this->lang->line('DISCOUNT_FIX');
		$this->a_validation_fields['bank_s'] = $this->lang->line('BANK_S');
		$this->a_validation_fields['touroperator_commision'] = $this->lang->line('OPERATOR_COMMISION');
		$this->a_validation_fields['touroperator_koeff'] = $this->lang->line('OPERATOR_KOEFF');
		// Tours
		$this->a_validation_fields['_touroperators_rid'] = $this->lang->line('TOUROPERATOR');         
		$this->a_validation_fields['_food_rid'] = $this->lang->line('FOOD');       
		$this->a_validation_fields['_rooms_rid'] = $this->lang->line('ROOM');
		$this->a_validation_fields['room_cat'] = $this->lang->line('CROOM');
		$this->a_validation_fields['_countries_rid'] = $this->lang->line('COUNTRY');
		$this->a_validation_fields['_curourts_rid'] = $this->lang->line('CUROURT');
		$this->a_validation_fields['_advertisessources_rid'] = $this->lang->line('ADVERTISE');
		$this->a_validation_fields['_hotelscats_rid'] = $this->lang->line('HOTELCAT');
		$this->a_validation_fields['_clients_rid'] = $this->lang->line('CLIENTS_LIST');
		$this->a_validation_fields['_zk_rid'] = $this->lang->line('ZAKAZCHIK');
		$this->a_validation_fields['_zk_is_tourist'] = '';
		$this->a_validation_fields['city_name'] = $this->lang->line('CITY');
		$this->a_validation_fields['hotel_name'] = $this->lang->line('HOTEL');
		$this->a_validation_fields['date_from'] = $this->lang->line('DATE_FROM');
		$this->a_validation_fields['date_to'] = $this->lang->line('DATE_TO'); 
		$this->a_validation_fields['transfer'] = $this->lang->line('TRANSFER');
		$this->a_validation_fields['excursions'] = $this->lang->line('EXCURSIONS');
		$this->a_validation_fields['order_image'] = $this->lang->line('ORDER');
		$this->a_validation_fields['cif_name'] = $this->lang->line('CIF');   		
		$this->a_validation_fields['_tcurrencies_rid'] = $this->lang->line('CURRENCY');
		$this->a_validation_fields['doxod'] = $this->lang->line('DOXOD');
		$this->a_validation_fields['fdoxod'] = $this->lang->line('FDOXOD');
		$this->a_validation_fields['rest'] = $this->lang->line('REST');
		// Rows
		$this->a_validation_fields['_clients_rid'] = $this->lang->line('CLIENT');
		$this->a_validation_fields['_calls_headers_rid'] = '';
		
		$this->a_validation_fields['descr'] = $this->lang->line('DESCR');
		$this->a_validation_fields['archive'] = $this->lang->line('ARCHIVE');
		$this->a_validation_fields['modifyDT'] = $this->lang->line('ARCHIVE');
		$this->validation->set_rules($this->a_validation_rules);
		$this->validation->set_fields($this->a_validation_fields);
		
		
		$this->objectsList['MENU_OBJECT'] = $this->menucontainer->RenderVerticalTreeMenu();
		
	}
	
	public function _remap($m_cif_name){
		switch ($m_cif_name) {
			case 'nJ': {echo $this->_create();break;}
			case 'eJ': {echo $this->_edit();break;}
			case 'dJ': {echo $this->_details();break;}
			case 'rJ': {echo $this->_remove();break;}
			case 'fJ'; {echo $this->_find();break;}
			case 'uJ'; {echo $this->_refresh();break;}
			case 'uC'; {echo $this->_refreshclients();break;}
			case 'srt'; {$this->_sort();break;}
			case 'ar'; {$this->_activerecord();break;}
			case 'sar'; {$this->_setactiverecord();break;}
			case 'pD': {$this->_printDemand();break;}
			case 'pA': {$this->_printAgreement();break;}
			case 'pC': {$this->_printCalc();break;}
			case 'pg':{echo $this->_journal();break;}	
			case 'dO':{$this->_showorder();break;}	
			case 'uTK':{echo $this->_updatekoeff();break;}		
			case 'uTC':{echo $this->_updatecommision();break;}
			default: $this->_index();
		}
	}
	
	protected function _index(){
		$this->objectsList['GRID_OBJECT'] = $this->_journal();
		$this->load->view('layouts/main_layout', $this->objectsList);
	}

	public function GetTools(){
		$tools = parent::GetTools();
		$tools['pdemand'] = $this->load->view('demands/tools/pdemand', null, True);
		$tools['pagreement'] = $this->load->view('demands/tools/pagreement', null, True);		        
		$tools['pcalc'] = $this->load->view('demands/tools/pcalc', null, True);
        return $tools;
	}
	
	public function _journal(){
		
		$content = array();
		$content['title'] = $this->lang->line('DICTIONARY_TITLE'); 
		$content['rid'] = 'ID'; 

		// Header
		$content['date_doc'] = $this->lang->line('DATE_DOC');
		$content['demandsstatuses_name'] = $this->lang->line('STATUS');
		$content['currency_code'] = $this->lang->line('CURRENCY');  
		$content['agreement'] = $this->lang->line('AGREEMENT'); 
		$content['summ'] = $this->lang->line('SUMM'); 
		$content['summ_text'] = $this->lang->line('SUMM_TEXT'); 
		$content['summ_pay'] = $this->lang->line('SUMM_PAY');
		// Tours
		$content['food_name'] = $this->lang->line('FOOD');       
		$content['room_name'] = $this->lang->line('ROOM');
		$content['country_name'] = $this->lang->line('COUNTRY');
		$content['curourt_name'] = $this->lang->line('CUROURT');
		$content['city_name'] = $this->lang->line('CITY');
		$content['hotelcat_name'] = $this->lang->line('HOTELCAT');
		$content['hotel_name'] = $this->lang->line('HOTEL');
		$content['date_from'] = $this->lang->line('DATE_FROM');
		$content['date_to'] = $this->lang->line('DATE_TO'); 
		$content['transfer'] = $this->lang->line('TRANSFER');
		$content['excursions'] = $this->lang->line('EXCURSIONS');
		$content['cif_name'] = $this->lang->line('CIF');   		
		// Rows
		$content['clients_quan'] = $this->lang->line('CLIENTS_QUAN');
				
		$content['archive'] =  $this->lang->line('ARCHIVE');
		$content['modifyDT'] =  $this->lang->line('MODIFYDT');
		$content['yes'] = $this->lang->line('YES');
		$content['no'] = $this->lang->line('NO');
		$content['tools'] = $this->GetTools(); 
		$content['DS'] = $this->demands_model->GetDS();
		$content['pagination'] = $this->GetPagination();
		if($content['DS'] && !$this->GetSessionData('activerecord')) $this->SetSessionData('activerecord', $content['DS'][0]->rid);
		$content['activerecord'] = $this->GetSessionData('activerecord');	
		//echo $content['activerecord'];
		return $this->load->view('demands/journal', $content, True);		
	}
	
	private function _create(){
		if ($this->validation->run() == FALSE){
			$callDS = null;
			$_calls_headers_rid = '';
			if($this->uri->segment(3)=='fC' && $callRid = $this->uri->segment(4)){
				$this->load->model('calls_model');
				$callDS = $this->calls_model->GetEditDS($callRid);
				$_calls_headers_rid = $callDS->_calls_headers_rid;
			}
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			
			# { _calls_headers_rid
			$content['_calls_headers_rid_field'] =  form_input(array('name'=>'_calls_headers_rid', 'type'=>'hidden', 'value'=>$this->validation->_calls_headers_rid?$this->validation->_calls_headers_rid:$_calls_headers_rid));
			# } _calls_headers_rid
			
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data_date_doc = array('name'=>'date_doc',
							'id'=>'date_doc',
							'value'=>$this->validation->date_doc?$this->validation->date_doc:date('d.m.Y'),
							'size'=>'12',
							'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data_date_doc);
			# } date_doc
			
			# { agreement
			$content['agreement'] = $this->RenderLabel('agreement');
			$data_agreement = array('name'=>'agreement',
							'id'=>'agreement',
							'value'=>$this->validation->agreement?$this->validation->agreement:'',
							'size'=>'17',
							'readonly'=>'readonly');
			$content['agreement_field'] =  form_input($data_agreement);
			# } agreement
			
			# { _demandsstatuses_rid
			$demandsstatuses = array(' '=>'');
			foreach($this->demands_model->GetDemandsstatusesList() as $demandstatuse){
				$demandsstatuses[$demandstatuse->rid] = $demandstatuse->demandsstatuses_name;
			}
			$content['_demandsstatuses_rid'] = $this->RenderLabel('_demandsstatuses_rid');
			$content['_demandsstatuses_rid_field'] =  form_dropdown('_demandsstatuses_rid', $demandsstatuses, $this->validation->_demandsstatuses_rid?$this->validation->_demandsstatuses_rid:'');
			# } _demandsstatuses_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$_advertisessources_rid = ($callDS)?$callDS->_advertisessources_rid:'';
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			//$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:'');
			$content['_currencies_rid_field'] =  form_hidden('_currencies_rid', '1');
			# } _currencies_rid
			
			# { summ
			$content['summ'] = $this->RenderLabel('summ');
			$content['summ_field'] =  form_input(array('name'=>'summ', 'id'=>'summ',  'value'=>$this->validation->summ?$this->validation->summ:'', 'size'=>'15'));
			# } summ
			
			# { summ_text
			$content['summ_text'] =$this->RenderLabel('summ_text');
			$content['summ_text_field'] =  form_input(array('name'=>'summ_text', 'id'=>'summ_text', 'value'=>$this->validation->summ_text?$this->validation->summ_text:'', 'size'=>'70'));
			# } summ_text
			
			# { summ_pay
			$content['summ_pay'] = $this->RenderLabel('summ_pay');
			$content['summ_pay_field'] =  form_input(array('name'=>'summ_pay', 'id'=>'summ_pay', 'value'=>$this->validation->summ_pay?$this->validation->summ_pay:'', 'size'=>'15'));
			# } summ_pay
			
			# { summ_tour 
			$content['summ_tour'] =  $this->RenderLabel('summ_tour');
			$content['summ_tour_field'] =  form_input(array('name'=>'summ_tour', 'id'=>'summ_tour', 'value'=>$this->validation->summ_tour?$this->validation->summ_tour:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } summ_tour


			# { order_summ 
			$content['order_summ'] =  $this->RenderLabel('order_summ');
			$content['order_summ_field'] =  form_input(array('name'=>'order_summ', 'id'=>'order_summ', 'value'=>$this->validation->order_summ?$this->validation->order_summ:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } order_summ

			# { fdoxod 
			$content['fdoxod'] =  $this->RenderLabel('fdoxod');
			$content['fdoxod_field'] =  form_input(array('name'=>'fdoxod', 'id'=>'fdoxod', 'value'=>$this->validation->fdoxod?$this->validation->fdoxod:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } fdoxod

			# { rest 
			$content['rest'] =  $this->RenderLabel('rest');
			$content['rest_field'] =  form_input(array('name'=>'rest', 'id'=>'rest', 'value'=>$this->validation->rest?$this->validation->rest:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } rest
			
			# { _tcurrencies_rid 
			$content['touroperator_koeff_updater'] = site_url().'/demands/uTK/';
			$content['_tcurrencies_rid'] =  $this->RenderLabel('_tcurrencies_rid');
			$content['_tcurrencies_rid_field'] =  form_dropdown('_tcurrencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_tcurrencies_rid?$this->validation->_tcurrencies_rid:'', "id=\"_tcurrencies_rid\"	onchange = \"new Ajax.Request('".$content['touroperator_koeff_updater']."'+document.getElementById('_touroperators_rid').value+'/'+document.getElementById('date_doc').value+'/'+this.value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_koeff').value = transport.responseText; }});RecalcForm();\"");
			# } _tcurrencies_rid
			
			# { curr_cource
			$content['curr_cource'] =  $this->RenderLabel('curr_cource');
			$content['curr_cource_field'] =  form_input(array('name'=>'curr_cource', 'id'=>'curr_cource', 'value'=>$this->validation->curr_cource?$this->validation->curr_cource:0, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } curr_cource

			# { discount_per
			$content['discount_per'] =  $this->RenderLabel('discount_per');
			$content['discount_per_field'] =  form_input(array('name'=>'discount_per', 'id'=>'discount_per', 'value'=>$this->validation->discount_per?$this->validation->discount_per:0, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_per

			# { discount_fix
			$content['discount_fix'] =  $this->RenderLabel('discount_fix');
			$content['discount_fix_field'] =  form_input(array('name'=>'discount_fix', 'id'=>'discount_fix', 'value'=>$this->validation->discount_fix?$this->validation->discount_fix:0, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_fix

			# { bank_s
			$content['bank_s'] =  $this->RenderLabel('bank_s');
			$content['bank_s_field'] =  form_input(array('name'=>'bank_s', 'id'=>'bank_s', 'value'=>$this->validation->bank_s?$this->validation->bank_s:0, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } bank_s
			
			# { touroperator_commision
			$content['touroperator_commision'] =  $this->RenderLabel('touroperator_commision');
			$content['touroperator_commision_updater'] = site_url().'/demands/uTC/';  
			$content['touroperator_commision_field'] =  form_input(array('name'=>'touroperator_commision', 'id'=>'touroperator_commision', 'value'=>$this->validation->touroperator_commision?$this->validation->touroperator_commision:0, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } touroperator_commision
			
			# { touroperator_koeff
			$content['touroperator_koeff'] =  $this->RenderLabel('touroperator_koeff');
			
			$content['touroperator_koeff_field'] =  form_input(array('name'=>'touroperator_koeff', 'id'=>'touroperator_koeff', 'value'=>$this->validation->touroperator_koeff?$this->validation->touroperator_koeff:0, 'size'=>'15'));
			# } touroperator_koeff

			# { doxod
			$content['doxod'] =  $this->RenderLabel('doxod');
			$content['doxod_field'] =  form_input(array('name'=>'doxod', 'id'=>'doxod', 'size'=>'15', 'readonly'=>'redaonly'));
			# } doxod
			
			# { _touroperators_rid
			$content['_touroperators_rid'] = $this->RenderLabel('_touroperators_rid');
			$content['_touroperators_rid_field'] =  form_dropdown('_touroperators_rid', 
																	$this->touroperators_model->GetPairArr(), 
																	$this->validation->_touroperators_rid?$this->validation->_touroperators_rid:'',
																	"id = \"_touroperators_rid\" onchange=\"new Ajax.Request('".$content['touroperator_commision_updater']."'+this.value+'/'+document.getElementById('date_doc').value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_commision').value = transport.responseText; }});
																	new Ajax.Request('".$content['touroperator_koeff_updater']."'+this.value+'/'+document.getElementById('date_doc').value+'/'+document.getElementById('_tcurrencies_rid').value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_koeff').value = transport.responseText; }});
																	\"");
			# } _touroperators_rid

			# { _food_rid
			$foods = array(' '=>'');
			foreach($this->demands_model->GetFoodsList() as $food){
				$foods[$food->rid] = $food->food_name;
			}
			$content['_food_rid'] = $this->RenderLabel('_food_rid');
			$content['_food_rid_field'] =  form_dropdown('_food_rid', $foods, $this->validation->_food_rid?$this->validation->_food_rid:'');
			# } _food_rid
			
			# { _rooms_rid
			$rooms = array(' '=>'');
			foreach($this->demands_model->GetRoomsList() as $room){
				$rooms[$room->rid] = $room->room_name;
			}
			$content['_rooms_rid'] = $this->RenderLabel('_rooms_rid');
			$content['_rooms_rid_field'] =  form_dropdown('_rooms_rid', $rooms, $this->validation->_rooms_rid?$this->validation->_rooms_rid:'');
			# } _rooms_rid

			# { room_cat
			$content['room_cat'] = $this->RenderLabel('room_cat');
			$data = array('name'=>'room_cat', 'id'=>'room_cat', 'size'=>'20', 'value'=>$this->validation->room_cat?$this->validation->room_cat:'');
			$content['room_cat_field'] =  form_input($data);
			# } room_cat
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->demands_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->RenderLabel('_countries_rid');
			$_countries_rid = ($callDS)?$callDS->_countries_rid:'';
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$_countries_rid);
			# } _countries_rid
			
			# { _curourts_rid
			$curourts = array(' '=>'');
			foreach($this->demands_model->GetCurourtsList() as $curourt){
				$curourts[$curourt->rid] = $curourt->curourt_name;
			}
			$content['_curourts_rid'] = $this->RenderLabel('_curourts_rid');
			$content['_curourts_rid_field'] =  form_dropdown('_curourts_rid', $curourts, $this->validation->_curourts_rid?$this->validation->_curourts_rid:'');
			# } _curourts_rid
			
			# { _hotelscats_rid
			$hotelscats = array(' '=>'');
			foreach($this->demands_model->GetHotelscatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->hotelcat_name;
			}
			$content['_hotelscats_rid'] = $this->RenderLabel('_hotelscats_rid');
			$content['_hotelscats_rid_field'] =  form_dropdown('_hotelscats_rid', $hotelscats, $this->validation->_hotelscats_rid?$this->validation->_hotelscats_rid:'');
			# } _hotelscats_rid
			
			# { city_name
			$content['city_name'] = $this->RenderLabel('city_name');
			$data = array('name'=>'city_name', 'id'=>'city_name', 'value'=>$this->validation->city_name?$this->validation->city_name:'', 'size'=>'20');
			$content['city_name_field'] =  form_input($data);
			# } city_name
			
			# { hotel_name
			$content['hotel_name'] = $this->RenderLabel('hotel_name');
			$data = array('name'=>'hotel_name', 'id'=>'hotel_name', 'value'=>$this->validation->hotel_name?$this->validation->hotel_name:'', 'size'=>'20');
			$content['hotel_name_field'] =  form_input($data);
			# } hotel_name
			
			# { date_from
			$content['date_from'] = $this->RenderLabel('date_from');
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:'', 'size'=>'15', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# { date_from
			
			# { date_to
			$content['date_to'] = $this->RenderLabel('date_to');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:'', 'size'=>'15', 'readonly'=>'readonly');  		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { transfer
			$content['transfer'] = $this->RenderLabel('transfer');	
			$data = array('name'=>'transfer', 'id'=>'transfer', 'value'=>$this->validation->transfer?$this->validation->transfer:'', 'size'=>'20');
			$content['transfer_field'] =  form_input($data);
			# } transfer
			
			# { excursions
			$content['excursions'] = $this->RenderLabel('excursions');
			$data = array('name'=>'excursions', 'id'=>'excursions', 'value'=>$this->validation->excursions?$this->validation->excursions:'', 'cols'=>'83', 'rows'=>'2');
			$content['excursions_field'] =  form_textarea($data);
			# } excursions

			# { order_image
			$content['order_image'] = $this->RenderLabel('order_image');
			$data = array('name'=>'order_image', 'id'=>'order_image', 'value'=>$this->validation->order_image?$this->validation->order_image:'', 'size'=>'25');
			$content['order_image_field'] =  form_upload($data);
			# } order_image
			
			# { _zk_rid
			$content['_zk_rid'] =  $this->validation->_zk_rid?$this->validation->_zk_rid:'';
			# } _zk_rid

			# { _zk_is_tourist
			$content['_zk_is_tourist'] =  $this->validation->_zk_is_tourist?$this->validation->_zk_is_tourist:'';
			# } _zk_is_tourist
			
			$content['_clients_rid_field'] = form_input('_clients_rid', $this->validation->_clients_rid?$this->validation->_clients_rid:'', 'id="_clients_rid" type="text"');
			// { Table Part
			$content['table'] = $this->_table($this->validation->_clients_rid?$this->validation->_clients_rid:'');
			// { Table Part

			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:''); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:'');
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:'');
			# } modifyDT
			return $this->load->view('demands/create', $content, True);
		}
		if($insRid = $this->demands_model->CreateRecord()){
			$this->SetSessionData('activerecord', $insRid);
			$this->load->view('standart/tmpl/create_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _edit(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		if ($this->validation->run() == FALSE){
			$content = array();
			$content['DST'] = $this->demands_model->GetEditDS();
			$content['DS'] = $content['DST'][0];
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data_date_doc = array('name'=>'date_doc',
							'id'=>'date_doc',
							'value'=>$this->validation->date_doc?$this->validation->date_doc:$content['DS']->date_doc,
							'size'=>'12',
							'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data_date_doc);
			# } date_doc
			
			# { _calls_headers_rid
			//$content['_calls_headers_rid_field'] =  form_input(array('name'=>'_calls_headers_rid', 'type'=>'hidden', 'value'=>$this->validation->_calls_headers_rid?$this->validation->_calls_headers_rid:$content['DS']->_calls_headers_rid));
			# } _calls_headers_rid
			
			# { agreement
			$content['agreement'] = $this->RenderLabel('agreement');
			$data_agreement = array('name'=>'agreement',
							'id'=>'agreement',
							'value'=>$this->validation->agreement?$this->validation->agreement:$content['DS']->agreement,
							'size'=>'17',
							'readonly'=>'readonly');
			$content['agreement_field'] =  form_input($data_agreement);
			# } agreement
			
			# { _demandsstatuses_rid
			$demandsstatuses = array(' '=>'');
			foreach($this->demands_model->GetDemandsstatusesList() as $demandstatuse){
				$demandsstatuses[$demandstatuse->rid] = $demandstatuse->demandsstatuses_name;
			}
			$content['_demandsstatuses_rid'] = $this->RenderLabel('_demandsstatuses_rid');
			$content['_demandsstatuses_rid_field'] =  form_dropdown('_demandsstatuses_rid', $demandsstatuses, $this->validation->_demandsstatuses_rid?$this->validation->_demandsstatuses_rid:$content['DS']->_demandsstatuses_rid);
			# } _demandsstatuses_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$content['DS']->_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->RenderLabel('_currencies_rid');
			//$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			$content['_currencies_rid_field'] =  form_hidden('_currencies_rid', '1');
			# } _currencies_rid
			
			# { summ
			$content['summ'] = $this->RenderLabel('summ');
			$content['summ_field'] =  form_input(array('name'=>'summ', 
														'id'=>'summ',  
														'value'=>$this->validation->summ?$this->validation->summ:$content['DS']->summ, 
														'size'=>'15'));
			# } summ
			
			# { summ_text
			$content['summ_text'] = $this->RenderLabel('summ_text');
			$content['summ_text_field'] =  form_input(array('name'=>'summ_text', 
															'id'=>'summ_text', 
															'value'=>$this->validation->summ_text?$this->validation->summ_text:$content['DS']->summ_text, 
															'size'=>'70'));
			# } summ_text
			
			# { summ_pay
			$content['summ_pay'] = $this->RenderLabel('summ_pay');
			$content['summ_pay_field'] =  form_input(array('name'=>'summ_pay', 'id'=>'summ_pay', 'value'=>$this->validation->summ_pay?$this->validation->summ_pay:$content['DS']->summ_pay, 'size'=>'15'));
			# } summ_pay
			
			# { summ_tour 
			$content['summ_tour'] =  $this->RenderLabel('summ_tour');
			$content['summ_tour_field'] =  form_input(array('name'=>'summ_tour', 
															'id'=>'summ_tour', 
															'value'=>$this->validation->summ_tour?$this->validation->summ_tour:$content['DS']->summ_tour, 
															'size'=>'15',
															'onChange'=>"RecalcForm();"));
			# } summ_tour

			# { order_summ 
			$content['order_summ'] =  $this->RenderLabel('order_summ');
			$content['order_summ_field'] =  form_input(array('name'=>'order_summ', 'id'=>'order_summ', 'value'=>$this->validation->order_summ?$this->validation->order_summ:$content['DS']->order_summ, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } order_summ

			# { fdoxod 
			$content['fdoxod'] =  $this->RenderLabel('fdoxod');
			$content['fdoxod_field'] =  form_input(array('name'=>'fdoxod', 'id'=>'fdoxod', 'value'=>$this->validation->fdoxod?$this->validation->fdoxod:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } fdoxod

			# { rest 
			$content['rest'] =  $this->RenderLabel('rest');
			$content['rest_field'] =  form_input(array('name'=>'rest', 'id'=>'rest', 'value'=>$this->validation->rest?$this->validation->rest:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } rest
			
			# { _tcurrencies_rid 
			$content['touroperator_koeff_updater'] = site_url().'/demands/uTK/';
			$content['_tcurrencies_rid'] =  $this->RenderLabel('_tcurrencies_rid');
			$content['_tcurrencies_rid_field'] =  form_dropdown('_tcurrencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_tcurrencies_rid?$this->validation->_tcurrencies_rid:$content['DS']->_tcurrencies_rid, "id=\"_tcurrencies_rid\"	onchange = \"new Ajax.Request('".$content['touroperator_koeff_updater']."'+document.getElementById('_touroperators_rid').value+'/'+document.getElementById('date_doc').value+'/'+this.value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_koeff').value = transport.responseText; }});RecalcForm();\"");
			# } _tcurrencies_rid
			
			# { curr_cource
			$content['curr_cource'] =  $this->RenderLabel('curr_cource');
			$content['curr_cource_field'] =  form_input(array('name'=>'curr_cource', 'id'=>'curr_cource', 'value'=>$this->validation->curr_cource?$this->validation->curr_cource:$content['DS']->curr_cource, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } curr_cource

			# { discount_per
			$content['discount_per'] =  $this->RenderLabel('discount_per');
			$content['discount_per_field'] =  form_input(array('name'=>'discount_per', 'id'=>'discount_per', 'value'=>$this->validation->discount_per?$this->validation->discount_per:$content['DS']->discount_per, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_per

			# { discount_fix
			$content['discount_fix'] =  $this->RenderLabel('discount_fix');
			$content['discount_fix_field'] =  form_input(array('name'=>'discount_fix', 'id'=>'discount_fix', 'value'=>$this->validation->discount_fix?$this->validation->discount_fix:$content['DS']->discount_fix, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_fix

			# { bank_s
			$content['bank_s'] =  $this->RenderLabel('bank_s');
			$content['bank_s_field'] =  form_input(array('name'=>'bank_s', 'id'=>'bank_s', 'value'=>$this->validation->bank_s?$this->validation->bank_s:$content['DS']->bank_s, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } bank_s
			
			# { doxod
			$content['doxod'] =  $this->RenderLabel('doxod');
			$content['doxod_field'] =  form_input(array('name'=>'doxod', 'id'=>'doxod', 'size'=>'15', 'readonly'=>'redaonly'));
			# } doxod
			
			$this->load->model('touroperatorscomisions_model');
			$commision = $this->touroperatorscomisions_model->GetTouroperatorCommision($this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid, $content['DS']->date_doc);
			# { touroperator_commision
			$content['touroperator_commision'] =  $this->RenderLabel('touroperator_commision');
			$content['touroperator_commision_updater'] = site_url().'/demands/uTC/';  
			$content['touroperator_commision_field'] =  form_input(array('name'=>'touroperator_commision', 'id'=>'touroperator_commision', 'value'=>$this->validation->touroperator_commision?$this->validation->touroperator_commision:$content['DS']->touroperator_commision, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } touroperator_commision
			
			$this->load->model('touroperatorskoeffs_model');
			$koeff = $this->touroperatorskoeffs_model->GetTouroperatorKoeff($this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid, $content['DS']->date_doc);
			# { touroperator_koeff
			$content['touroperator_koeff'] =  $this->RenderLabel('touroperator_koeff');
			  
			$content['touroperator_koeff_field'] =  form_input(array('name'=>'touroperator_koeff', 'id'=>'touroperator_koeff', 'value'=>$this->validation->touroperator_koeff?$this->validation->touroperator_koeff:$content['DS']->touroperator_koeff, 'size'=>'15'));
			# } touroperator_koeff
			
			# { _touroperators_rid
			$content['_touroperators_rid'] = $this->RenderLabel('_touroperators_rid');
			$content['_touroperators_rid_field'] =  form_dropdown('_touroperators_rid', 
																	$this->touroperators_model->GetPairArr(),
																	$this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid,
																	"id = \"_touroperators_rid\" onchange=\"new Ajax.Request('".$content['touroperator_commision_updater']."'+this.value+'/'+document.getElementById('date_doc').value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_commision').value = transport.responseText; }});
																	new Ajax.Request('".$content['touroperator_koeff_updater']."'+this.value+'/'+document.getElementById('date_doc').value+'/'+document.getElementById('_tcurrencies_rid').value, { method: 'post', onSuccess: function (transport){document.getElementById('touroperator_koeff').value = transport.responseText; }});
																	\"");
			# } _touroperators_rid

			# { _food_rid
			$foods = array(' '=>'');
			foreach($this->demands_model->GetFoodsList() as $food){
				$foods[$food->rid] = $food->food_name;
			}
			$content['_food_rid'] = $this->RenderLabel('_food_rid');
			$content['_food_rid_field'] =  form_dropdown('_food_rid', $foods, $this->validation->_food_rid?$this->validation->_food_rid:$content['DS']->_food_rid);
			# } _food_rid
			
			# { _rooms_rid
			$rooms = array(' '=>'');
			foreach($this->demands_model->GetRoomsList() as $room){
				$rooms[$room->rid] = $room->room_name;
			}
			$content['_rooms_rid'] = $this->RenderLabel('_rooms_rid');
			$content['_rooms_rid_field'] =  form_dropdown('_rooms_rid', $rooms, $this->validation->_rooms_rid?$this->validation->_rooms_rid:$content['DS']->_rooms_rid);
			# } _rooms_rid
			
			
			# { room_cat
			$content['room_cat'] = $this->RenderLabel('room_cat');
			$data = array('name'=>'room_cat', 'id'=>'room_cat', 'size'=>'20', 'value'=>$this->validation->room_cat?$this->validation->room_cat:$content['DS']->room_cat);
			$content['room_cat_field'] =  form_input($data);
			# } room_cat
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->demands_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->RenderLabel('_countries_rid');
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# } _countries_rid
			
			# { _curourts_rid
			$curourts = array(' '=>'');
			foreach($this->demands_model->GetCurourtsList() as $curourt){
				$curourts[$curourt->rid] = $curourt->curourt_name;
			}
			$content['_curourts_rid'] = $this->RenderLabel('_curourts_rid');
			$content['_curourts_rid_field'] =  form_dropdown('_curourts_rid', $curourts, $this->validation->_curourts_rid?$this->validation->_curourts_rid:$content['DS']->_curourts_rid);
			# } _curourts_rid
			
			# { _hotelscats_rid
			$hotelscats = array(' '=>'');
			foreach($this->demands_model->GetHotelscatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->hotelcat_name;
			}
			$content['_hotelscats_rid'] = $this->RenderLabel('_hotelscats_rid');
			$content['_hotelscats_rid_field'] =  form_dropdown('_hotelscats_rid', $hotelscats, $this->validation->_hotelscats_rid?$this->validation->_hotelscats_rid:$content['DS']->_hotelscats_rid);
			# } _hotelscats_rid
			
			# { city_name
			$content['city_name'] = $this->RenderLabel('city_name');
			$data = array('name'=>'city_name', 'id'=>'city_name', 'value'=>$this->validation->city_name?$this->validation->city_name:$content['DS']->city_name, 'size'=>'20');
			$content['city_name_field'] =  form_input($data);
			# } city_name
			
			# { hotel_name
			$content['hotel_name'] = $this->RenderLabel('hotel_name');
			$data = array('name'=>'hotel_name', 'id'=>'hotel_name', 'value'=>$this->validation->hotel_name?$this->validation->hotel_name:$content['DS']->hotel_name, 'size'=>'20');
			$content['hotel_name_field'] =  form_input($data);
			# } hotel_name
			
			# { date_from
			$content['date_from'] = $this->RenderLabel('date_from');
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:$content['DS']->date_from, 'size'=>'15', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# { date_from
			
			# { date_to
			$content['date_to'] = $this->RenderLabel('date_to');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:$content['DS']->date_to, 'size'=>'15', 'readonly'=>'readonly');  		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { transfer
			$content['transfer'] = $this->RenderLabel('transfer');
			$data = array('name'=>'transfer', 'id'=>'transfer', 'value'=>$this->validation->transfer?$this->validation->transfer:$content['DS']->transfer, 'size'=>'20');
			$content['transfer_field'] =  form_input($data);
			# } transfer
			
			# { excursions
			$content['excursions'] = $this->RenderLabel('excursions');	
			$data = array('name'=>'excursions', 'id'=>'excursions', 'value'=>$this->validation->excursions?$this->validation->excursions:$content['DS']->excursions, 'cols'=>'83', 'rows'=>'2');
			$content['excursions_field'] =  form_textarea($data);
			# } excursions

			# { order_image
			$content['order_image'] = $this->RenderLabel('order_image');
			$data = array('name'=>'order_image', 'id'=>'order_image', 'size'=>'25');
			$content['order_image_field'] =  form_upload($data);
			if(!empty($content['DS']->order_image) && !empty($content['DS']->order_name) && !empty($content['DS']->order_type) && !empty($content['DS']->order_size)){
				$content['order_image_field'] .= anchor(site_url().'/demands/dO', $this->lang->line('SHOWIMAGE'));			
			}
			# } order_image
			
			# { _zk_rid
			$content['_zk_rid'] =  $this->validation->_zk_rid?$this->validation->_zk_rid:$content['DS']->_zk_rid;
			# } _zk_rid

			# { _zk_is_tourist
			$content['_zk_is_tourist'] =  $this->validation->_zk_is_tourist?$this->validation->_zk_is_tourist:$content['DS']->_zk_is_tourist;
			# } _zk_is_tourist
									
			$content['_clients_rid'] = array();
			foreach($content['DST'] as $row) $content['_clients_rid'][] = $row->_clients_rid;
			$content['_clients_rid'][] = $content['DS']->_zk_rid; 
			$content['_clients_rid'] = implode('-', array_unique($content['_clients_rid']));
			
			// { Table Part
			$content['table'] = $this->_table($content['_clients_rid']);
			// } Table Part
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('demands/edit', $content, True);
		}
		if($this->demands_model->UpdateRecord()){
			$this->load->view('standart/tmpl/edit_success', array('identifier'=>$this->GetIdentifier()));
		}
	}
	
	private function _details(){
			if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
			$content = array();
			$content['DST'] = $this->demands_model->GetEditDS();
			$content['DS'] = $content['DST'][0];
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_CREATE')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_CREATE');
			# { date_doc
			$content['date_doc'] = $this->lang->line('DATE_DOC');
			$data_date_doc = array('name'=>'date_doc',
							'id'=>'date_doc',
							'value'=>$this->validation->date_doc?$this->validation->date_doc:date('d.m.Y'),
							'size'=>'12',
							'readonly'=>'readonly');
			$content['date_doc_field'] =  form_input($data_date_doc);
			# } date_doc
			
			# { agreement
			$content['agreement'] = $this->lang->line('AGREEMENT');
			$data_agreement = array('name'=>'agreement',
							'id'=>'agreement',
							'value'=>$this->validation->agreement?$this->validation->agreement:$content['DS']->agreement,
							'size'=>'17',
							'readonly'=>'readonly');
			$content['agreement_field'] =  form_input($data_agreement);
			# } agreement
			
			# { _demandsstatuses_rid
			$demandsstatuses = array(' '=>'');
			foreach($this->demands_model->GetDemandsstatusesList() as $demandstatuse){
				$demandsstatuses[$demandstatuse->rid] = $demandstatuse->demandsstatuses_name;
			}
			$content['_demandsstatuses_rid'] = $this->lang->line('STATUS');
			$content['_demandsstatuses_rid_field'] =  form_dropdown('_demandsstatuses_rid', $demandsstatuses, $this->validation->_demandsstatuses_rid?$this->validation->_demandsstatuses_rid:$content['DS']->_demandsstatuses_rid);
			# } _demandsstatuses_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$content['DS']->_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->lang->line('CURRENCY'); 
			//$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_currencies_rid?$this->validation->_currencies_rid:$content['DS']->_currencies_rid);
			$content['_currencies_rid_field'] =  form_hidden('_currencies_rid', '1');
			# } _currencies_rid
			
			# { summ
			$content['summ'] = $this->lang->line('SUMM');
			$content['summ_field'] =  form_input(array('name'=>'summ', 'id'=>'summ',  'value'=>$this->validation->summ?$this->validation->summ:$content['DS']->summ, 'size'=>'15'));
			# } summ
			
			# { summ_text
			$content['summ_text'] = $this->lang->line('SUMM_TEXT');
			$content['summ_text_field'] =  form_input(array('name'=>'summ_text', 'id'=>'summ_text', 'value'=>$this->validation->summ_text?$this->validation->summ_text:$content['DS']->summ_text, 'size'=>'70'));
			# } summ_text
			
			# { summ_pay
			$content['summ_pay'] = $this->lang->line('SUMM_PAY');
			$content['summ_pay_field'] =  form_input(array('name'=>'summ_pay', 'id'=>'summ_pay', 'value'=>$this->validation->summ_pay?$this->validation->summ_pay:$content['DS']->summ_pay, 'size'=>'15'));
			# } summ_pay
			
			# { summ_tour 
			$content['summ_tour'] =  $this->RenderLabel('summ_tour');
			$content['summ_tour_field'] =  form_input(array('name'=>'summ_tour', 
															'id'=>'summ_tour', 
															'value'=>$this->validation->summ_tour?$this->validation->summ_tour:$content['DS']->summ_tour, 
															'size'=>'15',
															'onChange'=>"RecalcForm();"));
			# } summ_tour

			# { order_summ 
			$content['order_summ'] =  $this->RenderLabel('order_summ');
			$content['order_summ_field'] =  form_input(array('name'=>'order_summ', 'id'=>'order_summ', 'value'=>$this->validation->order_summ?$this->validation->order_summ:$content['DS']->order_summ, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } order_summ

			# { fdoxod 
			$content['fdoxod'] =  $this->RenderLabel('fdoxod');
			$content['fdoxod_field'] =  form_input(array('name'=>'fdoxod', 'id'=>'fdoxod', 'value'=>$this->validation->fdoxod?$this->validation->fdoxod:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } fdoxod

			# { rest 
			$content['rest'] =  $this->RenderLabel('rest');
			$content['rest_field'] =  form_input(array('name'=>'rest', 'id'=>'rest', 'value'=>$this->validation->rest?$this->validation->rest:'', 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } rest
			
			# { _tcurrencies_rid 
			$content['_tcurrencies_rid'] =  $this->RenderLabel('_tcurrencies_rid');
			$content['_tcurrencies_rid_field'] =  form_dropdown('_tcurrencies_rid', $this->currencies_model->GetPairArr(), $this->validation->_tcurrencies_rid?$this->validation->_tcurrencies_rid:$content['DS']->_tcurrencies_rid);
			# } _tcurrencies_rid
			
			# { curr_cource
			$content['curr_cource'] =  $this->RenderLabel('curr_cource');
			$content['curr_cource_field'] =  form_input(array('name'=>'curr_cource', 'id'=>'curr_cource', 'value'=>$this->validation->curr_cource?$this->validation->curr_cource:$content['DS']->curr_cource, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } curr_cource

			# { discount_per
			$content['discount_per'] =  $this->RenderLabel('discount_per');
			$content['discount_per_field'] =  form_input(array('name'=>'discount_per', 'id'=>'discount_per', 'value'=>$this->validation->discount_per?$this->validation->discount_per:$content['DS']->discount_per, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_per

			# { discount_fix
			$content['discount_fix'] =  $this->RenderLabel('discount_fix');
			$content['discount_fix_field'] =  form_input(array('name'=>'discount_fix', 'id'=>'discount_fix', 'value'=>$this->validation->discount_fix?$this->validation->discount_fix:$content['DS']->discount_fix, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } discount_fix

			# { bank_s
			$content['bank_s'] =  $this->RenderLabel('bank_s');
			$content['bank_s_field'] =  form_input(array('name'=>'bank_s', 'id'=>'bank_s', 'value'=>$this->validation->bank_s?$this->validation->bank_s:$content['DS']->bank_s, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } bank_s
			
			# { doxod
			$content['doxod'] =  $this->RenderLabel('doxod');
			$content['doxod_field'] =  form_input(array('name'=>'doxod', 'id'=>'doxod', 'size'=>'15', 'readonly'=>'redaonly'));
			# } doxod
			
			$this->load->model('touroperatorscomisions_model');
			$commision = $this->touroperatorscomisions_model->GetTouroperatorCommision($this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid, $content['DS']->date_doc);
			# { touroperator_commision
			$content['touroperator_commision'] =  $this->RenderLabel('touroperator_commision');
			$content['touroperator_commision_updater'] = site_url().'/demands/uTC/';  
			$content['touroperator_commision_field'] =  form_input(array('name'=>'touroperator_commision', 'id'=>'touroperator_commision', 'value'=>$this->validation->touroperator_commision?$this->validation->touroperator_commision:$content['DS']->touroperator_commision, 'size'=>'15', 'onChange'=>'RecalcForm();'));
			# } touroperator_commision
			
			$this->load->model('touroperatorskoeffs_model');
			$koeff = $this->touroperatorskoeffs_model->GetTouroperatorKoeff($this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid, $content['DS']->date_doc);
			# { touroperator_koeff
			$content['touroperator_koeff'] =  $this->RenderLabel('touroperator_koeff');
			$content['touroperator_koeff_updater'] = site_url().'/demands/uTK/';  
			$content['touroperator_koeff_field'] =  form_input(array('name'=>'touroperator_koeff', 'id'=>'touroperator_koeff', 'value'=>$this->validation->touroperator_koeff?$this->validation->touroperator_koeff:$content['DS']->touroperator_koeff, 'size'=>'15'));
			# } touroperator_koeff
						
			# { _touroperators_rid
			$content['_touroperators_rid'] = $this->lang->line('TOUROPERATOR'); 
			$content['_touroperators_rid_field'] =  form_dropdown('_touroperators_rid', $this->touroperators_model->GetPairArr(), $this->validation->_touroperators_rid?$this->validation->_touroperators_rid:$content['DS']->_touroperators_rid);
			# } _touroperators_rid

			# { _food_rid
			$foods = array(' '=>'');
			foreach($this->demands_model->GetFoodsList() as $food){
				$foods[$food->rid] = $food->food_name;
			}
			$content['_food_rid'] = $this->lang->line('FOOD'); 
			$content['_food_rid_field'] =  form_dropdown('_food_rid', $foods, $this->validation->_food_rid?$this->validation->_food_rid:$content['DS']->_food_rid);
			# } _food_rid
			
			# { _rooms_rid
			$rooms = array(' '=>'');
			foreach($this->demands_model->GetRoomsList() as $room){
				$rooms[$room->rid] = $room->room_name;
			}
			$content['_rooms_rid'] = $this->lang->line('ROOM'); 
			$content['_rooms_rid_field'] =  form_dropdown('_rooms_rid', $rooms, $this->validation->_rooms_rid?$this->validation->_rooms_rid:$content['DS']->_rooms_rid);
			# } _rooms_rid
			
			# { room_cat
			$content['room_cat'] = $this->RenderLabel('room_cat');
			$data = array('name'=>'room_cat', 'id'=>'room_cat', 'size'=>'20', 'value'=>$this->validation->room_cat?$this->validation->room_cat:$content['DS']->room_cat);
			$content['room_cat_field'] =  form_input($data);
			# } room_cat
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->demands_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->lang->line('COUNTRY'); 
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries, $this->validation->_countries_rid?$this->validation->_countries_rid:$content['DS']->_countries_rid);
			# } _countries_rid
			
			# { _curourts_rid
			$curourts = array(' '=>'');
			foreach($this->demands_model->GetCurourtsList() as $curourt){
				$curourts[$curourt->rid] = $curourt->curourt_name;
			}
			$content['_curourts_rid'] = $this->lang->line('CUROURT'); 
			$content['_curourts_rid_field'] =  form_dropdown('_curourts_rid', $curourts, $this->validation->_curourts_rid?$this->validation->_curourts_rid:$content['DS']->_curourts_rid);
			# } _curourts_rid
			
			# { _hotelscats_rid
			$hotelscats = array(' '=>'');
			foreach($this->demands_model->GetHotelscatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->hotelcat_name;
			}
			$content['_hotelscats_rid'] = $this->lang->line('HOTELCAT'); 
			$content['_hotelscats_rid_field'] =  form_dropdown('_hotelscats_rid', $hotelscats, $this->validation->_hotelscats_rid?$this->validation->_hotelscats_rid:$content['DS']->_hotelscats_rid);
			# } _hotelscats_rid
			
			# { city_name
			$content['city_name'] = $this->lang->line('CITY'); 
			$data = array('name'=>'city_name', 'id'=>'city_name', 'value'=>$this->validation->city_name?$this->validation->city_name:$content['DS']->city_name, 'size'=>'20');
			$content['city_name_field'] =  form_input($data);
			# } city_name
			
			# { hotel_name
			$content['hotel_name'] = $this->lang->line('HOTEL'); 
			$data = array('name'=>'hotel_name', 'id'=>'hotel_name', 'value'=>$this->validation->hotel_name?$this->validation->hotel_name:$content['DS']->hotel_name, 'size'=>'20');
			$content['hotel_name_field'] =  form_input($data);
			# } hotel_name
			
			# { date_from
			$content['date_from'] = $this->lang->line('DATE_FROM'); 
			$data = array('name'=>'date_from', 'id'=>'date_from', 'value'=>$this->validation->date_from?$this->validation->date_from:$content['DS']->date_from, 'size'=>'15', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# { date_from
			
			# { date_to
			$content['date_to'] = $this->lang->line('DATE_TO');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'value'=>$this->validation->date_to?$this->validation->date_to:$content['DS']->date_to, 'size'=>'15', 'readonly'=>'readonly');  		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { transfer
			$content['transfer'] = $this->lang->line('TRANSFER'); 		
			$data = array('name'=>'transfer', 'id'=>'transfer', 'value'=>$this->validation->transfer?$this->validation->transfer:$content['DS']->transfer, 'size'=>'20');
			$content['transfer_field'] =  form_input($data);
			# } transfer
			
			# { excursions
			$content['excursions'] = $this->lang->line('EXCURSIONS'); 	
			$data = array('name'=>'excursions', 'id'=>'excursions', 'value'=>$this->validation->excursions?$this->validation->excursions:$content['DS']->excursions, 'cols'=>'83', 'rows'=>'2');
			$content['excursions_field'] =  form_textarea($data);
			# } excursions

			# { order_image
			$content['order_image'] = $this->RenderLabel('order_image');
			$data = array('name'=>'order_image', 'id'=>'order_image', 'size'=>'25');
			$content['order_image_field'] =  form_upload($data);
			# } order_image
			
			# { _zk_rid
			$content['_zk_rid'] =  $this->validation->_zk_rid?$this->validation->_zk_rid:$content['DS']->_zk_rid;
			# } _zk_rid

			# { _zk_is_tourist
			$content['_zk_is_tourist'] =  $this->validation->_zk_is_tourist?$this->validation->_zk_is_tourist:$content['DS']->_zk_is_tourist;
			# } _zk_is_tourist
												
			$content['_clients_rid'] = array();
			foreach($content['DST'] as $row) $content['_clients_rid'][] = $row->_clients_rid;
			$content['_clients_rid'][] = $content['DS']->_zk_rid; 
			$content['_clients_rid'] = implode('-', array_unique($content['_clients_rid']));
			
			// { Table Part
			$content['table'] = $this->_table($content['_clients_rid']);
			// } Table Part
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2', 'value'=>$this->validation->descr?$this->validation->descr:$content['DS']->descr); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array('0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')), $this->validation->archive?$this->validation->archive:$content['DS']->archive);
			# } archive
			
			# { modifyDT
			$content['modifyDT'] =  $this->lang->line('MODIFYDT');
			$content['modifyDT_field'] =  form_input('modifyDT', $this->validation->modifyDT?$this->validation->modifyDT:$content['DS']->modifyDT);
			# } modifyDT
			return $this->load->view('demands/details', $content, True);
	}

	private function _remove(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->demands_model->DeleteRecord();
		$this->SetSessionData('activerecord', null);
		echo $this->_journal();
	}
	
	private function _find(){
		if (!isset($_POST['goFind'])){
			$content = array();
			$content['header'] = $this->load->view('standart/tmpl/header', array('title'=>$this->lang->line('DICTIONARY_TITLE_FIND')), True);
			$content['title'] = $this->lang->line('DICTIONARY_TITLE_FIND'); 
			
			# { date_doc_from
			$content['date_doc_from'] = $this->lang->line('DATE_DOC_FROM');
			$data = array('name'=>'date_doc_from', 'id'=>'date_doc_from', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_from_field'] = form_input($data).$this->RenderCalendar('date_doc_from');
			# } date_doc_from

			# { date_doc_to
			$content['date_doc_to'] = $this->lang->line('DATE_DOC_TO');
			$data = array('name'=>'date_doc_to', 'id'=>'date_doc_to', 'readonly'=>'readonly', 'size'=>'12'); 
			$content['date_doc_to_field'] = form_input($data).$this->RenderCalendar('date_doc_to');
			# } date_doc_to
									
			# { agreement
			$content['agreement'] = $this->lang->line('AGREEMENT');
			$data_agreement = array('name'=>'agreement',
							'id'=>'agreement',
							'size'=>'17',
							'readonly'=>'readonly');
			$content['agreement_field'] =  form_input($data_agreement);
			# } agreement
			
			# { _demandsstatuses_rid
			$demandsstatuses = array(' '=>'');
			foreach($this->demands_model->GetDemandsstatusesList() as $demandstatuse){
				$demandsstatuses[$demandstatuse->rid] = $demandstatuse->demandsstatuses_name;
			}
			$content['_demandsstatuses_rid'] = $this->lang->line('STATUS');
			$content['_demandsstatuses_rid_field'] =  form_dropdown('_demandsstatuses_rid', $demandsstatuses);
			# } _demandsstatuses_rid
			
			# { _advertisessources_rid
			$content['_advertisessources_rid'] = $this->RenderLabel('_advertisessources_rid');
			$content['_advertisessources_rid_field'] =  form_dropdown('_advertisessources_rid', $this->advertisessources_model->GetPairArr(), $this->validation->_advertisessources_rid?$this->validation->_advertisessources_rid:$content['DS']->_advertisessources_rid);
			# } _advertisessources_rid
						
			# { _currencies_rid
			$content['_currencies_rid'] = $this->lang->line('CURRENCY'); 
			$content['_currencies_rid_field'] =  form_dropdown('_currencies_rid', $this->currencies_model->GetPairArr());
			# } _currencies_rid
			
			# { summ_from
			$content['summ_from'] = $this->lang->line('SUMM_FROM');
			$content['summ_from_field'] =  form_input(array('name'=>'summ_from', 'id'=>'summ_from', 'size'=>'15'));
			# } summ_from

			# { summ_to
			$content['summ_to'] = $this->lang->line('SUMM_TO');
			$content['summ_to_field'] =  form_input(array('name'=>'summ_to', 'id'=>'summ_to', 'size'=>'15'));
			# } summ_to
			
			# { summ_pay_from
			$content['summ_pay_from'] = $this->lang->line('SUMM_PAY_FROM');
			$content['summ_pay_from_field'] =  form_input(array('name'=>'summ_pay_from', 'id'=>'summ_pay_from', 'size'=>'15'));
			# } summ_pay_from

			# { summ_pay_to
			$content['summ_pay_to'] = $this->lang->line('SUMM_PAY_TO');
			$content['summ_pay_to_field'] =  form_input(array('name'=>'summ_pay_to', 'id'=>'summ_pay_to', 'size'=>'15'));
			# } summ_pay_to
			
			# { _touroperators_rid
			$touroperators = array(' '=>'');
			foreach($this->demands_model->GetTouroperatorsList() as $touroperator){
				$touroperators[$touroperator->rid] = $touroperator->touroperator_name;
			}
			$content['_touroperators_rid'] = $this->lang->line('TOUROPERATOR'); 
			$content['_touroperators_rid_field'] =  form_dropdown('_touroperators_rid', $touroperators);
			# } _touroperators_rid

			# { _food_rid
			$foods = array(' '=>'');
			foreach($this->demands_model->GetFoodsList() as $food){
				$foods[$food->rid] = $food->food_name;
			}
			$content['_food_rid'] = $this->lang->line('FOOD'); 
			$content['_food_rid_field'] =  form_dropdown('_food_rid', $foods);
			# } _food_rid
			
			# { _rooms_rid
			$rooms = array(' '=>'');
			foreach($this->demands_model->GetRoomsList() as $room){
				$rooms[$room->rid] = $room->room_name;
			}
			$content['_rooms_rid'] = $this->lang->line('ROOM'); 
			$content['_rooms_rid_field'] =  form_dropdown('_rooms_rid', $rooms);
			# } _rooms_rid
			
			# { _countries_rid
			$countries = array(' '=>'');
			foreach($this->demands_model->GetCountriesList() as $country){
				$countries[$country->rid] = $country->country_name;
			}
			$content['_countries_rid'] = $this->lang->line('COUNTRY'); 
			$content['_countries_rid_field'] =  form_dropdown('_countries_rid', $countries);
			# } _countries_rid
			
			# { _curourts_rid
			$curourts = array(' '=>'');
			foreach($this->demands_model->GetCurourtsList() as $curourt){
				$curourts[$curourt->rid] = $curourt->curourt_name;
			}
			$content['_curourts_rid'] = $this->lang->line('CUROURT'); 
			$content['_curourts_rid_field'] =  form_dropdown('_curourts_rid', $curourts);
			# } _curourts_rid
			
			# { _hotelscats_rid
			$hotelscats = array(' '=>'');
			foreach($this->demands_model->GetHotelscatsList() as $hotelcat){
				$hotelscats[$hotelcat->rid] = $hotelcat->hotelcat_name;
			}
			$content['_hotelscats_rid'] = $this->lang->line('HOTELCAT'); 
			$content['_hotelscats_rid_field'] =  form_dropdown('_hotelscats_rid', $hotelscats);
			# } _hotelscats_rid
			
			# { city_name
			$content['city_name'] = $this->lang->line('CITY'); 
			$data = array('name'=>'city_name', 'id'=>'city_name', 'size'=>'20');
			$content['city_name_field'] =  form_input($data);
			# } city_name
			
			# { hotel_name
			$content['hotel_name'] = $this->lang->line('HOTEL'); 
			$data = array('name'=>'hotel_name', 'id'=>'hotel_name', 'size'=>'20');
			$content['hotel_name_field'] =  form_input($data);
			# } hotel_name
			
			# { date_from
			$content['date_from'] = $this->lang->line('DATE_FROM'); 
			$data = array('name'=>'date_from', 'id'=>'date_from', 'size'=>'15', 'readonly'=>'readonly');		
			$content['date_from_field'] =  form_input($data).$this->RenderCalendar('date_from');
			# { date_from
			
			# { date_to
			$content['date_to'] = $this->lang->line('DATE_TO');
			$data = array('name'=>'date_to', 'id'=>'date_to', 'size'=>'15', 'readonly'=>'readonly');  		
			$content['date_to_field'] =  form_input($data).$this->RenderCalendar('date_to');
			# } date_to
			
			# { transfer
			$content['transfer'] = $this->lang->line('TRANSFER'); 		
			$data = array('name'=>'transfer', 'id'=>'transfer', 'size'=>'20');
			$content['transfer_field'] =  form_input($data);
			# } transfer
			
			# { excursions
			$content['excursions'] = $this->lang->line('EXCURSIONS'); 	
			$data = array('name'=>'excursions', 'id'=>'excursions', 'size'=>'25');
			$content['excursions_field'] =  form_input($data);
			# } excursions
			
			# { cl_f_name
			$content['cl_f_name'] = $this->lang->line('CL_F_NAME'); 
			$data = array('name'=>'cl_f_name', 'id'=>'cl_f_name', 'size'=>'20');
			$content['cl_f_name_field'] =  form_input($data);
			# } cl_f_name

			# { cl_s_name
			$content['cl_l_name'] = $this->lang->line('CL_l_NAME'); 
			$data = array('name'=>'cl_l_name', 'id'=>'cl_l_name', 'size'=>'20');
			$content['cl_l_name_field'] =  form_input($data);
			# } cl_s_name

			# { cl_passp_seria
			$content['cl_passp_seria'] = $this->lang->line('CL_PASSP_SERIA'); 
			$data = array('name'=>'cl_passp_seria', 'id'=>'cl_passp_seria', 'size'=>'20');
			$content['cl_passp_seria_field'] =  form_input($data);
			# } cl_passp_seria

			# { cl_passp_num
			$content['cl_passp_num'] = $this->lang->line('CL_PASSP_NUM'); 
			$data = array('name'=>'cl_passp_num', 'id'=>'cl_passp_num', 'size'=>'20');
			$content['cl_passp_num_field'] =  form_input($data);
			# } cl_passp_num
			
			# { descr
			$content['descr'] =  $this->lang->line('DESCR');
			$data = array('name'=>'descr', 'id'=>'descr', 'cols'=>'50', 'rows'=>'2'); 
			$content['descr_field'] =  form_textarea($data);
			# } descr

			# { archive
			$content['archive'] =  $this->lang->line('ARCHIVE');
			$content['archive_field'] =  form_dropdown('archive', array(' '=>'','0'=>$this->lang->line('NO'), '1'=>$this->lang->line('YES')));
			# } archive
						
			return $this->load->view('demands/find', $content, True);			
		}
		$searchRule = array();
		if(isset($_POST['date_doc_from']) && trim($_POST['date_doc_from'])) $searchRule['_demands_headers.date_doc>='] = trim($_POST['date_doc_from']);
		if(isset($_POST['date_doc_to']) && trim($_POST['date_doc_to'])) $searchRule['_demands_headers.date_doc<='] = trim($_POST['date_doc_to']);
		if(isset($_POST['agreement']) && trim($_POST['agreement'])) $searchRule['_demands_headers.agreement'] = trim($_POST['agreement']);
		if(isset($_POST['_demandsstatuses_rid']) && trim($_POST['_demandsstatuses_rid'])) $searchRule['_demands_headers._demandsstatuses_rid'] = trim($_POST['_demandsstatuses_rid']);
		if(isset($_POST['_advertisessources_rid']) && trim($_POST['_advertisessources_rid'])) $searchRule['_demands_headers._advertisessources_rid'] = trim($_POST['_advertisessources_rid']);
		if(isset($_POST['_currencies_rid']) && trim($_POST['_currencies_rid'])) $searchRule['_demands_headers._currencies_rid'] = trim($_POST['_currencies_rid']);
		if(isset($_POST['summ_from']) && trim($_POST['summ_from'])) $searchRule['_demands_headers.summ>='] = trim($_POST['summ_from']);
		if(isset($_POST['summ_to']) && trim($_POST['summ_to'])) $searchRule['_demands_headers.summ<='] = trim($_POST['summ_to']);
		if(isset($_POST['summ_pay_from']) && trim($_POST['summ_pay_from'])) $searchRule['_demands_headers.summ_pay>='] = trim($_POST['summ_pay_from']);
		if(isset($_POST['summ_pay_to']) && trim($_POST['summ_pay_to'])) $searchRule['_demands_headers.summ_pay<='] = trim($_POST['summ_pay_to']);
		if(isset($_POST['_touroperators_rid']) && trim($_POST['_touroperators_rid'])) $searchRule['_demands_headers._touroperators_rid'] = trim($_POST['_touroperators_rid']);
		if(isset($_POST['_food_rid']) && trim($_POST['_food_rid'])) $searchRule['_tours._food_rid'] = trim($_POST['_food_rid']);
		if(isset($_POST['_rooms_rid']) && trim($_POST['_rooms_rid'])) $searchRule['_tours._rooms_rid'] = trim($_POST['_rooms_rid']);
		if(isset($_POST['_countries_rid']) && trim($_POST['_countries_rid'])) $searchRule['_tours._countries_rid'] = trim($_POST['_countries_rid']);
		if(isset($_POST['_curourts_rid']) && trim($_POST['_curourts_rid'])) $searchRule['_tours._curourts_rid'] = trim($_POST['_curourts_rid']);
		if(isset($_POST['_hotelscats_rid']) && trim($_POST['_hotelscats_rid'])) $searchRule['_tours._hotelscats_rid'] = trim($_POST['_hotelscats_rid']);
		if(isset($_POST['city_name']) && trim($_POST['city_name'])) $searchRule['_tours.city_name'] = trim($_POST['city_name']);
		if(isset($_POST['hotel_name']) && trim($_POST['hotel_name'])) $searchRule['_tours.hotel_name'] = trim($_POST['hotel_name']);
		if(isset($_POST['transfer']) && trim($_POST['transfer'])) $searchRule['_tours.transfer'] = trim($_POST['transfer']);
		if(isset($_POST['excursions']) && trim($_POST['excursions'])) $searchRule['_tours.excursions'] = trim($_POST['excursions']);
		if(isset($_POST['f_name']) && trim($_POST['f_name'])) $searchRule['_clients.f_name'] = trim($_POST['f_name']);
		if(isset($_POST['l_name']) && trim($_POST['l_name'])) $searchRule['_clients.l_name'] = trim($_POST['l_name']);
		if(isset($_POST['passp_seria']) && trim($_POST['passp_seria'])) $searchRule['_clients.passp_seria'] = trim($_POST['passp_seria']);
		if(isset($_POST['passp_num']) && trim($_POST['passp_num'])) $searchRule['_clients.passp_num'] = trim($_POST['passp_num']);
		if(isset($_POST['date_from']) && trim($_POST['date_from'])) $searchRule['_tours.date_from<='] = trim($_POST['date_from']);
		if(isset($_POST['date_to']) && trim($_POST['date_to'])) $searchRule['_tours.date_to>='] = trim($_POST['date_to']);
		if(isset($_POST['descr']) && trim($_POST['descr'])) $searchRule['_documents.descr'] = trim($_POST['descr']);
		if(isset($_POST['archive']) && trim($_POST['archive'])) $searchRule['_documents.archive'] = trim($_POST['archive']);
		if($searchRule){
			$this->_setsearchrule($searchRule);
		}
		if($_POST['goFind']=='2'){
			redirect('demands', 'refresh');
		}
		$this->load->view('standart/tmpl/find_success', array('identifier'=>$this->GetIdentifier()));
	}
	
	private function _refresh(){
		$this->RefreshSessionData();
		echo $this->_journal();
	}
	
	private function _table($p_clients=''){
		// { Table Part
		$content['DST'] = $this->demands_model->GetClientsDS($p_clients);
		if(!$content['DST']) $content['DST'] = array();
		$content['_clients_rid'] = $p_clients;
		$content['tools_clients']['add'] = $this->load->view('demands/tools/cladd', null, True);
		$content['tools_clients']['remove'] = $this->load->view('demands/tools/clremove', null, True);
		$content['tools_clients']['edit'] = $this->load->view('standart/tools/edit', array('identifier'=>'clients', 'width'=>'600', 'height'=>'700'), True);
		$content['tools_clients']['details'] = $this->load->view('standart/tools/details', array('identifier'=>'clients', 'width'=>'600', 'height'=>'700'), True);
		$content['f_name'] = $this->lang->line('F_NAME'); 	
		$content['s_name'] = $this->lang->line('S_NAME'); 	
		$content['l_name'] = $this->lang->line('L_NAME'); 	
		$content['birthday'] = $this->lang->line('BIRTHDAY'); 	
		$content['f_pass_seria'] = $this->lang->line('FPASSP_SERIA'); 	
		$content['f_pass_num'] = $this->lang->line('FPASSP_NUM'); 	
		$content['f_pass_period'] = $this->lang->line('FPASSP_PERIOD'); 	
		$content['zk'] = $this->lang->line('ZK');
		$content['tourist'] = $this->lang->line('TOURIST'); 	
		// { Table Part
		return $this->load->view('demands/table', $content, True);
	}
	
	private function _refreshclients(){
		return $this->_table($this->uri->segment(3));
	}
	
	private function _printDemand(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->load->library('demand_pdf');
		$ds = $this->demands_model->GetEditDS();
		$zkRid = $ds[0]->_zk_rid;
		$this->demand_pdf->SetDS($ds);
		$ds = $this->demands_model->GetFilialDS();
		$this->demand_pdf->SetFilial($ds);		
		$ds = $this->user->GetFullName();
		$this->demand_pdf->SetManager($ds);
		$ds = $this->demands_model->GetOrderer($zkRid);
		$this->demand_pdf->SetOrderer($ds);
		$this->demand_pdf->GetDemand();
		die();
	}

	private function _printAgreement(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->load->library('agreement_pdf');
		$ds = $this->demands_model->GetEditDS();
		$zkRid = $ds[0]->_zk_rid;
		$this->agreement_pdf->SetDS($ds);
		$this->agreement_pdf->SetFilial($this->demands_model->GetFilialDS());		
		$this->agreement_pdf->SetManager($this->user->GetFullName());
		$this->agreement_pdf->SetOrderer($this->demands_model->GetOrderer($zkRid));
		$this->agreement_pdf->GetAgreement();
		die();
		
	}
	
	private function _showorder(){
		$ds = $this->demands_model->GetEditDS();
 		header("Content-length: {$ds[0]->order_size}");
		header("Content-type: {$ds[0]->order_type}");
		header("Content-Disposition: attachment; filename={$ds[0]->order_name}");
		echo $ds[0]->order_image;		
	}
	
	private function _printCalc(){
		if($activerecord=$this->uri->segment(3)) $this->_setactiverecord($activerecord);
		$this->load->library('calc_pdf');
		$ds = $this->demands_model->GetEditDS();
		$tourRid = $ds[0]->_tours_rid;
		$this->calc_pdf->SetDS($ds);
		$this->calc_pdf->SetFilial($this->demands_model->GetFilialDS());		
		$this->calc_pdf->SetManager($this->user->GetFullName());
		$this->calc_pdf->SetTourCurrency($this->demands_model->GetTourCurrency($tourRid));
		$this->load->model('emptopercantage_model');
		$this->load->model('outgoingincoming_model');
		$commision = $this->emptopercantage_model->GetCommision($this->user->GetUserEmpRid());
		$this->calc_pdf->SetManagerCommision($commision);
		$this->calc_pdf->SetFinPR($this->outgoingincoming_model->GetDocFinOper($this->GetSessionData('activerecord'), '00003'));
		$this->calc_pdf->SetFinFO($this->outgoingincoming_model->GetDocFinOper($this->GetSessionData('activerecord'), '00004'));
		$this->calc_pdf->GetCalc();
		die();
		
	}
	
	private function _updatecommision(){
		$this->load->model('touroperatorscomisions_model');
		$tRid = $this->uri->segment(3);
		$tDArr = explode('.', $this->uri->segment(4));
		$tDate = implode('-', array_reverse($tDArr));
		$commision = $this->touroperatorscomisions_model->GetTouroperatorCommision($tRid, $tDate);
		# { touroperator_commision
		return  $commision?$commision->commision_value:'';
		# } touroperator_commision
	}
	
	private function _updatekoeff(){
		$this->load->model('touroperatorskoeffs_model');
		$tRid = $this->uri->segment(3);
		$cRid = $this->uri->segment(5);
		if(empty($cRid)) return;
		$tDArr = explode('.', $this->uri->segment(4));
		$tDate = implode('-', array_reverse($tDArr));
		$koeff = $this->touroperatorskoeffs_model->GetTouroperatorKoeff($tRid, $cRid, $tDate);
		# { touroperator_koeff
		return ($koeff)?$koeff->koeff_value:'';
		# } touroperator_koeff
	}
	
}

?>